"""
@Project:catering_takeout
@File:serializer.py
@Author:柴奇峰
@Date:11:03
"""
from rest_framework import serializers
from users.models import UserModel,Chat
from shops.models import Shops


# 沟通表序列化器
class CommunicationSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_name = serializers.SerializerMethodField()
    user_avatar_url = serializers.SerializerMethodField()
    shops = serializers.PrimaryKeyRelatedField(queryset=Shops.objects.all())
    shops_name = serializers.SerializerMethodField()
    shops_avatar_url = serializers.SerializerMethodField()
    def get_user_name(self, obj):
        return obj.user.nickname
    def get_user_avatar_url(self, obj):
        return obj.user.avatar_url
    def get_shops_name(self, obj):
        return obj.shops.nickname
    def get_shops_avatar_url(self, obj):
        return obj.shops.avatar_url
    def get_send_name(self, obj):
        if not obj.send:
            return None
        return obj.send.nickname

    class Meta:
        model = Chat
        fields = "__all__"
